;****************************************************************************************
;*   This file is the firmware to implement 
;*	2 timers, which on expiration can trigger 16 different single event routines
;*      furthermore the possibility of insertion of code into periodic routines 
;*	which occur every 64 microseconds, 8 milliseconds, 2 seconds and 16 seconds
;*
;*   Refer to the MPASM User's Guide for additional information on     			*
;*   features of the assembler (Document DS33014).                     			*
;*                                                                     			*
;*   Refer to the respective PICmicro data sheet for additional        			*
;*   information on the instruction set.                               			*
;*                                                                     			*
;********************************************************************************	
;*                                                                 			*
;*    Filename:	    MICROCHIP OS                               		                *
;*    Date:         December, 2015                                   			*
;*    File Version: A, this is a new file                                    		*
;*                                                                     			*
;*    Author:       G Michael Drygas                                      		*
;*                  Applications Engineer                    				*
;*    Company:                                  			*
;****************************************************************************************
;*                                			                                *
;*    Files required:   m0-4.asm (macros)                                          		*
;*                                                                     			*
;*                                                                     			*
;*                                                                     			*
;****************************************************************************************
;*                                                                     			*
;*    Notes:        hardware 	dc to ac board							*
;*		         					*
;*                                                                     			*
;****************************************************************************************
	list      p=10F206	; list directive to define processor
	#include  ..\m0-4.asm
	config_
 	
#define	OPTIONinit	b'11000001'	; no wakup, no pullup, Clock 1:4
#define TRISIOinit 	b'00001001'	; 0 input, 1,2 output
#define	ADCON0init	b'01000001'	; gP0 analog, chanel 00,enable
;#define	ADCON0init	b'10000101'	; gP1 analog, chanel 01,enable


;#define GPIOinit 4	b'00000000'	; GPIO  all LOW 
;****************************************************************************************

	init_			; reasonable initiation of special function registers 


	cblock	_MINRAM			; start of variables
	 SyS1,AlarmCount
	endc
#define STACK		_MAXRAM

#define	TMR07Bit	SyS1,7
	

;PWRUP	
		eq	SyS1,0
	    eq_ AlarmCount,=,.1,lit8

TC0used = 3							; use 3 bytes of timer for TC0
	cblock
	 TC0:TC0used
	 NmbrEvent0
	endc	 
START
		EVENT0in	.5000,msec,0xFF  ; set time @ 5000 milliseconds
		eq_			NmbrEvent0,=,.0,lit8	; set execution  for EVENT0
TC1used = 2
	if TC1used > 0
	cblock
	 TC1:TC1used,NmbrEvent1
	endc
		EVENT1in	.1500,msec,.15	;EVENT15 in 1.5 sec, goto MLOOP	 
	endif
JMPTable	andlw	0x0F
		addwf	PCL,F
		 goto	EVENT0	
		 goto	EVENT1
		 goto	EVENT2	
		 goto	EVENT3
		 goto	EVENT4
		 goto	EVENT5
		 goto	EVENT6
		 goto	EVENT7
		 goto	EVENT8
		 goto	EVENT9
		 goto	EVENT10
		 goto	EVENT11
		 goto	EVENT12
		 goto	EVENT13
		 goto	EVENT14
		 goto	EVENT15

;	org	0x100
	
MLOOP	clrwdt

	
	if_bit	TMR0,7			; software emulation of
	 goto	set7			; timer 0 overflow
	if_not	TMR07Bit		;
	 goto	set7+1
		bcf		TMR07Bit
		;	if_not	INTCON,T0IF		; wait for timer TMR0 to time out
		;	 goto	MLOOP
		;	bcf	INTCON,T0IF			
	goto	PERIODIC8		; which is 256*4 us =1 milliseconds
exitPERIODIC8
	decfsz	TC0,F
	 goto	endMLOOP0
;	goto	PERIODIC2048		; after first TC0 expired, 2048 milliseconds 
;exitPERIODIC2048			; have passed
	if TC0used > 1
;	movlw	0x07			;example for very long periodic events
;	andwf	TC0+1,W
;	movwf	STACK
;	movlw	-7
;	addwf	STACK,W
;	skpnZ
;	 goto	PERIODIC16sec		; every 16 seconds (Event0 must be later!)
;exitPERIODIC16sec
	decfsz	TC0+1,F
	 goto	endMLOOP0
	endif
					; after TC0+1 expired, 8 minutes 44 seconds
	if TC0used > 2			; have passed
	decfsz	TC0+2,F
	 goto	endMLOOP0
	endif 
	movfw	NmbrEvent0		; after TC0+2 expired, 37 hours 17 minutes
	goto	JMPTable 		; now jump to loaded Event
	 
endMLOOP0				; and continue execution here	 
end0MLOOP0
	if TC1used > 0
	decfsz	TC1,F			; analogous as TC0
	 goto	endMLOOP1
	endif
	if TC1used > 1
	decfsz	TC1+1,F
	 goto	endMLOOP1
	endif
	if TC1used > 2
	decfsz	TC1+2,F
	 goto	endMLOOP1
	endif 
	if TC1used > 0
	movfw	NmbrEvent1
	goto	JMPTable 
	endif

	goto	endMLOOP1 
	 
	 
set7	bsf	TMR07Bit	
endMLOOP1	goto	MLOOP

;	org	0x200
;PERIODIC64usec			
;		goto	exitPERIODIC64usec
;
PERIODIC8							; check fuse tripped for 255 times
;	movf	AlarmCount,f
;	skpNZ
;	 goto    exitPERIODIC8			; signal gen is in shutdown
;	if_m	GPIO,3,is_on,1
; 		incfsz	AlarmCount,F
;	      goto	increasedAlarm
;		bcf	GPIO,2
;		bcf	GPIO,1					; Alarm = 0
;		EVENT0in	.3,sec,.4		; shut down! wait 30 sec
;	end_m
;	goto    exitPERIODIC8			; signal gen is running, with Alarm >= 1
;increasedAlarm
;	EVENT1in	.60,msec,0xFF
;	eq_			NmbrEvent1,=,.14,lit8
	goto    exitPERIODIC8			; signal gen is running, but waiting to collect
									; more Alarm, else decrease it in 60msec			
;;
;PERIODIC2048	
;		goto	exitPERIODIC2048
;		
;PERIODIC16sec	
;		goto	exitPERIODIC16sec	
;				 

#define fac .25

#define	hiSide	SyS1,2			 
EVENT0	
	bsf	ADCON0,GO
	btfsc	ADCON0,GO
	 goto	$-1	
	if_m
	
    if_m hiSide,is_on,1
	  bsf	GPIO,1
	else_m
	  bsf	GPIO,2
	end_m

	EVENT0in	.6600,usec,1
	
EVENT1	
	bcf	GPIO,1
	EVENT0in	.3400*fac,usec,2	
EVENT2			
	bsf	GPIO,2
	EVENT0in	.6600,usec,3		
EVENT3
	bcf	GPIO,2
	EVENT0in	.3400*fac,usec,0		 
EVENT4							; coming after shut down
	if_m	GPIO,3,is_off,1
	 eq_ AlarmCount,=,.1,lit8
     EVENT0in	.300,msec,.5
	end_m
	EVENT0in	.3,sec,.4		; 
EVENT5		
	if_m	GPIO,3,is_on,1
	  EVENT0in	.300,msec,.6
	end_m
	EVENT0in	.100,msec,.5
EVENT6	
	if_	GPIO,3,is_off,1
	  goto	EVENT0
	EVENT0in	.100,msec,.6
EVENT7		

EVENT8		

EVENT9		

EVENT10		

EVENT11		


EVENT12	
	

EVENT13
	

EVENT14 
	if_	GPIO,3,is_off,1			
	 decfsz	AlarmCount,F
    goto EVENT14A			; if no signal and Alarm>1
	incf	AlarmCount,F
	goto	EVENT15
EVENT14A
    EVENT1in .60,msec,.14	;come backin 60 msec

EVENT15 

	EVENT1in	6,sec,.15	;loop, Alarm=1


	
		end